package algotithm.leetcode.tree.test236;

import algotithm.leetcode.tree.TreeNode;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/8-23:40
 * 	236 二叉树的最近公共祖先
 */
public class Solution {

    public static void main(String[] args) {

    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || p == root || q == root) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if (left == null && right == null) {
            return null;
        }
        if (left == null) {
            return right;
        }
        if (right == null) {
            return left;
        }
        return root;
    }

}
